package net.sansi.v3correctserver.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.dto.R;
import net.sansi.v3correctserver.dto.correct.BrightCorrectDto;
import net.sansi.v3correctserver.exception.ServiceException;
import net.sansi.v3correctserver.service.correct.BrightCorrectService;
import net.sansi.v3correctserver.task.BrightCorrectTask;
import net.sansi.v3correctserver.task.V3Task;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * Created by Fang
 * create time : 2024/11/1
 * IDE : IntelliJ IDEA
 * <p>
 * 亮度矫正控制器
 */

@Api(tags = "亮度矫正接口")
@RestController
@RequestMapping("/bright-correct")
@Validated
public class BrightCorrectController {

    @Resource
    private BrightCorrectService brightCorrectService;

    /**
     * 矩形屏亮度矫正（返回任务ID）
     *
     * @param form 请求参数
     * @return 任务ID
     */
    @ApiOperation("矩形屏亮度矫正（返回任务ID）")
    @PostMapping("/rect/start")
    public R<String> rectStart(@RequestBody @Validated BrightCorrectDto.RectStartReq form) {
        return R.data(brightCorrectService.startTask(form));
    }

    /**
     * 查询矩形屏亮度矫正任务进度
     *
     * @param taskId 任务ID
     * @return 进度
     */
    @ApiOperation("查询矩形屏亮度矫正任务进度")
    @GetMapping("/rect/progress")
    public R<BrightCorrectTask.CorrectProgress> rectProgress(@RequestParam("taskId") @NotBlank() String taskId) {
        V3Task<?, ?> task = AppHelper.getTaskManager().get(taskId);
        if (task == null) throw new ServiceException("任务不存在 {}", taskId);
        return R.data((BrightCorrectTask.CorrectProgress) task.getProgress());
    }
}
